package  io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.R;
import io.renren.modules.generator.dao.VehicleDao;
import io.renren.modules.generator.entity.VehicleEntity;
import io.renren.modules.generator.service.VehicleService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;


@Service("vehicleService")
public class VehicleServiceImpl extends ServiceImpl<VehicleDao, VehicleEntity> implements VehicleService {

    @Autowired
    private  VehicleDao vehicleDao;

    @Override
    public  List<VehicleEntity> queryPage(Map<String, Object> params) {

        return vehicleDao.queryPage(params);
    }
    @Override
    public  int queryPageTotal(Map<String, Object> params) {

        return vehicleDao.queryPageTotal(params);
    }

    @Override
    public R importFile(Workbook book) {
        Sheet sheet = book.getSheetAt(0);
        Pattern compile = Pattern.compile("^[0-9]+([.]{1}[0-9]+){0,1}$");

        List<VehicleEntity> vehicleEntities = new ArrayList<>();

        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            VehicleEntity vehicleEntity = new VehicleEntity();

            Double inLength=null;
            Double inWidth=null;
            Double inHeight=null;

            Row row = sheet.getRow(i);
            if(row.getCell(0) != null && !row.getCell(0).toString().trim().equals("")){
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                String code =  row.getCell(0).getStringCellValue().trim();
                if(code.equals("end")){
                    break;
                }

                vehicleEntity.setCode(code);

            }else{
                return R.error("第"+i+"行车辆编码不能为空");
            }

            /**
             * 车辆名称
             */
            if(row.getCell(1) != null && !row.getCell(1).toString().trim().equals("")){
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                String name =  row.getCell(1).getStringCellValue().trim();
                vehicleEntity.setName(name);

            }else{
                return R.error("第"+i+"行车辆名称不能为空");
            }

            /**
             * 内径长
             */
            if(row.getCell(2) != null && !row.getCell(2).toString().trim().equals("")){
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(2).getStringCellValue().trim();
                BigDecimal one = new BigDecimal(value);
                String length = one.toPlainString();
                boolean matches = compile.matcher(length).matches();
                if(matches){
                    inLength = Double.valueOf(length);
                    vehicleEntity.setInLength(inLength);
                }else{
                    return R.error("第"+i+"行内径长请输入数字类型");
                }

            }else{
                return R.error("第"+i+"行内径长不能为空");
            }

            /**
             * 内径宽
             */
            if(row.getCell(3) != null && !row.getCell(3).toString().trim().equals("")){
                row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(3).getStringCellValue().trim();
                BigDecimal one = new BigDecimal(value);
                String width = one.toPlainString();
                boolean matches = compile.matcher(width).matches();
                if(matches){
                    inWidth = Double.valueOf(width);
                    vehicleEntity.setInWidth(inWidth);
                }else{
                    return R.error("第"+i+"行内径宽请输入数字类型");
                }

            }else{
                return R.error("第"+i+"行内径宽不能为空");
            }

            /**
             * 内径高
             */
            if(row.getCell(4) != null && !row.getCell(4).toString().trim().equals("")){
                row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(4).getStringCellValue().trim();
                BigDecimal one = new BigDecimal(value);
                String height = one.toPlainString();
                boolean matches = compile.matcher(height).matches();
                if(matches){
                    inHeight = Double.valueOf(height);
                    vehicleEntity.setInHeight(inHeight);
                }else {
                    return R.error("第"+i+"行内径高请输入数字类型");
                }

            }else{
                return R.error("第"+i+"行内径高不能为空");
            }

            vehicleEntity.setVolume(inHeight*inLength*inWidth);


            /**
             * 外径长
             */
            if(row.getCell(5) != null && !row.getCell(5).toString().trim().equals("")){
                row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(5).getStringCellValue().trim();
                BigDecimal one = new BigDecimal(value);
                String outLength = one.toPlainString();
                boolean matches = compile.matcher(outLength).matches();
                if(matches){
                    vehicleEntity.setOutLength(Double.valueOf(outLength));
                }else {
                    return R.error("第"+i+"行外径长请输入数字类型");
                }

            }

            /**
             * 外径宽
             */
            if(row.getCell(6) != null && !row.getCell(6).toString().trim().equals("")){
                row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(6).getStringCellValue().trim();
                BigDecimal one = new BigDecimal(value);
                String outWidth = one.toPlainString();
                boolean matches = compile.matcher(outWidth).matches();
                if(matches){
                    vehicleEntity.setOutWidth(Double.valueOf(outWidth));
                }else {
                    return R.error("第"+i+"行外径宽请输入数字类型");
                }

            }

            /**
             * 外径高
             */
            if(row.getCell(7) != null && !row.getCell(7).toString().trim().equals("")){
                row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(7).getStringCellValue().trim();
                BigDecimal one = new BigDecimal(value);
                String outHeight = one.toPlainString();
                boolean matches = compile.matcher(outHeight).matches();
                if(matches){
                    vehicleEntity.setOutHeight(Double.valueOf(outHeight));
                }else {
                    return R.error("第"+i+"行外径高请输入数字类型");
                }

            }

            /**
             * 载重
             */
            if(row.getCell(8) != null && !row.getCell(8).toString().trim().equals("")){
                row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(8).getStringCellValue().trim();
                BigDecimal one = new BigDecimal(value);
                String weight = one.toPlainString();
                boolean matches = compile.matcher(weight).matches();
                if(matches){
                    vehicleEntity.setWeight(Double.valueOf(weight));
                }else {
                    return R.error("第"+i+"行载重请输入数字类型");
                }

            }else{
                return R.error("第"+i+"行载重不能为空");
            }

            /**
             * 板数
             */
            if(row.getCell(9) != null && !row.getCell(9).toString().trim().equals("")){
                row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(9).getStringCellValue().trim();
                BigDecimal one = new BigDecimal(value);
                String numberOfPlates = one.toPlainString();
                boolean matches = compile.matcher(numberOfPlates).matches();
                if(matches){
                    vehicleEntity.setNumberOfPlates(Double.valueOf(numberOfPlates));
                }else {
                    return R.error("第"+i+"行板数请输入数字类型");
                }

            }else{
                return R.error("第"+i+"行板数不能为空");
            }


            /**
             * 备注
             */
            if(row.getCell(10) != null && !row.getCell(10).toString().trim().equals("")){
                row.getCell(10).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(10).getStringCellValue().trim();
                vehicleEntity.setRemark(value);

            }

            /**
             * 预留字段1
             */
            if(row.getCell(11) != null && !row.getCell(11).toString().trim().equals("")){
                row.getCell(11).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(11).getStringCellValue().trim();
                vehicleEntity.setExtendFirst(value);

            }

            /**
             * 预留字段2
             */
            if(row.getCell(12) != null && !row.getCell(12).toString().trim().equals("")){
                row.getCell(12).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(12).getStringCellValue().trim();
                vehicleEntity.setExtendSecond(value);

            }

            /**
             * 预留字段3
             */
            if(row.getCell(13) != null && !row.getCell(13).toString().trim().equals("")){
                row.getCell(13).setCellType(Cell.CELL_TYPE_STRING);
                String value =  row.getCell(13).getStringCellValue().trim();
                vehicleEntity.setExtendSecond(value);

            }

            vehicleEntities.add(vehicleEntity);
        }

        String code="";
        try{
            if(vehicleEntities.size() > 0){
                for (VehicleEntity vehicle :
                        vehicleEntities) {
                    code = vehicle.getCode();
                    this.insert(vehicle);
                }


            }
        }catch (DuplicateKeyException d){
            d.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new DuplicateKeyException("车辆编码[ "+code+" ]已经存在,请不要重复导入");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return R.ok();
    }


}
